#pragma
#include <iostream>
#include <cstdlib>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <error.h>
#include <cstring>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
const std::string PATHNAME = "/home/ljt/forgit/linux-learning-code/newfile24_10_11/shm";
const int projID = 0x12345678;
const int size = 4096;
const std::string filename = "fifo";
key_t GetKey()
{
    key_t key= ftok(PATHNAME.c_str(), projID);
    if(key < 0)
    {
        std::cerr << "errno:" << errno << ",errnostring: " << strerror(errno) << std::endl;
        exit(1);
    }
    return key;
}

int CreatShmHelper(key_t key, int flag)
{
    int shmid = shmget(key, size, flag);
    if(shmid < 0)
    {
        std::cerr << "errno:" << errno << ",errnostring: " << strerror(errno) << std::endl;
        exit(2);
    }
    return shmid;
}

int CreatShm(key_t key)
{
    return CreatShmHelper(key, IPC_CREAT|IPC_EXCL|0644);
}

int GetShm(key_t key)
{
    return CreatShmHelper(key, IPC_CREAT);
}

std::string ToHex(int x)
{
    char buffer[1024];

    snprintf(buffer, sizeof(buffer), "0x%x", x);
    return buffer;
}

bool MakeFifo()
{
    int n = mkfifo(filename.c_str(), 0666);
    if(n < 0)
    {
        std::cerr << "errno" << errno << ", errstring" << strerror(errno) << std::endl;
        return false;
    }
    std::cout << "Mkfifo success" << std::endl;
    return true;
}